package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //查询结果集
        List<Permission> permissions = permissionMapper.query(qo);
        return new PageInfo<Permission>(permissions);
    }

    @Autowired
    private ApplicationContext ctx; //spring 上下文对象

    @Override
    public JsonResult reload() {
        try {
            //查询出已经加载过得权限列表
            List<String> permissions = permissionMapper.selectAllPermission();
            //利用spring上下文,获取带有controller注解的所有bean
            Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
            Collection<Object> controllers = beans.values();
            //遍历每个controller,获取字节码对象,再获取该字节码中所有的方法
            for (Object controller : controllers) {
                Class<?> clazz = controller.getClass().getSuperclass();//获取字节码对象
                Method[] methods = clazz.getDeclaredMethods();//该字节码中所有的方法
                //遍历每个方法
                for (Method method : methods) {
                    //判断该方法是否有贴注解 @RequiredPermisson
                    RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                    //如果有贴注解
                    if (annotation != null) {
                        //使用工具类 生成当前这个方法的权限表达式
//                        String expression = PermissionUtil.handlerExpression(method);
                        //  不使用了 直接在shiro注解上获取 value值得 第0 第1 两个参数
                        String expression = annotation.value()[0];
                        String name = annotation.value()[1];
                        //判断 如果数据库中查询出来的权限表达式集合 没有这条数据就插入数据库
                        if (!permissions.contains(expression)) {
                            Permission permission = new Permission();
                            permission.setName(name);//权限名称
                            permission.setExpression(expression);//权限表达式
                            permissionMapper.insert(permission);//插入数据库
                        }
                    }
                }
            }
            return new JsonResult("加载成功", true);
        } catch (Exception e) {
            return new JsonResult("加载失败", false);
        }

    }

}
